<template>
  <div>
    <el-card>
      <el-form :model="searchForm" ref="searchFormRef" size="mini" :inline="true">
        <div>
          <el-form-item label="公司名称：">
            <el-input
              v-model="searchForm.enterpriseName"
              placeholder="请输入公司名称"
              style="width: 200px"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号码：">
            <el-input
            style="width: 200px"
              maxlength="11"
              v-model="searchForm.phoneNumber"
              placeholder="请输入手机号码"
              clearable
            ></el-input>
          </el-form-item>
          <!-- <el-form-item label="查询时间：">
            <el-date-picker
              v-model="searchForm.paymoneyDate1"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd"
              :default-value="defaultTime"
              format="yyyy 年 MM 月 dd 日"
            >
              >
            </el-date-picker>
          </el-form-item> -->
          <el-form-item label="查询时间：" prop="beginTime">
            <el-date-picker
            style="width: 200px"
              v-model="searchForm.beginTime"
              align="right"
              type="date"
              placeholder="选择开始日期"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd"
              format="yyyy 年 MM 月 dd 日"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>-</el-form-item>
          <el-form-item prop="endTime">
            <el-date-picker
            style="width: 200px"
              v-model="searchForm.endTime"
              align="right"
              type="date"
              placeholder="选择结束日期"
              :picker-options="pickerOptions"
              value-format="yyyy-MM-dd"
              format="yyyy 年 MM 月 dd 日"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="mini"
              @click="getlist('searchFormRef')"
              >查询</el-button
            >
            <el-button type="primary" size="small" @click="resetList()"
              >重置</el-button
            >
          </el-form-item>
        </div>
      </el-form>

      <div class="mt10">
        <el-table :data="tableData" border size="mini">
          <el-table-column label="序号" type="index" width="50">
          </el-table-column>
          <el-table-column prop="enterpriseName" label="公司名称" width="220">
          </el-table-column>
          <el-table-column prop="creditCode" label="纳税人识别号" width="250">
          </el-table-column>
          <el-table-column
            prop="legalPerson"
            label="法定代表人姓名"
            width="120"
          >
          </el-table-column>
          <el-table-column prop="phoneNumber" label="电话号码" width="180">
          </el-table-column>
          <el-table-column prop="province" label="所属地区" width="180">
          </el-table-column>
          <el-table-column prop="queryTime" label="历史查询时间" width="180">
          </el-table-column>
          <el-table-column label="操作" width="180">

            <template slot-scope="scope">
              <el-button v-if="scope.row.status == 200" type="text" @click="detail(scope.row)" size="small"
                >详情</el-button
              >
              <el-button type="text" @click="openDia(scope.row)" size="small"
                >更新数据</el-button
              >
              <!-- <el-button type="text" @click="updateData(scope.row)" size="small"
                >强制更新</el-button
              > -->
            </template>
          </el-table-column>
        </el-table>
        <!-- <el-button @click="updateData()" type="primary">强制跟新</el-button> -->
        <el-pagination
          class="pageRight"
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="100"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>
    <taskDetail ref="taskDetailRef"></taskDetail>
  </div>
</template>

  <script>
import store from "../../../store";
import { getHistoryList } from "@/api/tax";
import { timeFun } from "../../../utils/date";
import {authPhone} from '@/utils/auth.js'
import taskDetail from "./taskDetail.vue";
export default {
  components: {
    taskDetail,
  },
  data() {
    return {
      //日期配置项
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [
          {
            text: "今天",
            onClick(picker) {
              picker.$emit("pick", new Date());
            },
          },
          {
            text: "昨天",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一周前",
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", date);
            },
          },
          {
            text: "一年前",
            onClick(picker) {
              const date = new Date();
              date.setFullYear(date.getFullYear() - 1);
              picker.$emit("pick", date);
            },
          },
          {
            text: "两年前",
            onClick(picker) {
              const date = new Date();
              date.setFullYear(date.getFullYear() - 2);
              picker.$emit("pick", date);
            },
          },
          {
            text: "三年前",
            onClick(picker) {
              const date = new Date();
              date.setFullYear(date.getFullYear() - 3);
              picker.$emit("pick", date);
            },
          },
        ],
      },
      //查询条件
      searchForm: {
        paymoneyDate1: [],
        enterpriseName: "",
        phoneNumber: "",
        pageNum: 1,
        pageSize: 10,
        beginTime: "",
        endTime: "",
      },
      tableData: [],
      total: 0,
    };
  },
  activated() {
    this.searchList();
  },
  //销毁前重置
  deactivated() {
    this.tableData = [];
    this.total = 0;
    this.searchForm = {
      paymoneyDate1: [],
      enterpriseName: "",
      phoneNumber: "",
      pageNum: 1,
      pageSize: 10,
    };
  },
  methods: {
    detail(v) {
      this.$refs.taskDetailRef.openDia(v);
    },
    updateData(v) {
      store.commit('SET_ENTERPRISEID', v.enterpriseId)
      this.$emit("seedetail");
      // console.log(v);
    },
    //重置
    resetList() {
      this.searchForm = {
        paymoneyDate1: [],
        enterpriseName: "",
        phoneNumber: "",
        pageNum: 1,
        pageSize: 10,
      };
      this.searchList();
    },
    openDia(v) {
      this.$emit("seedetail", v);
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val;
      this.searchList();
    },
    handleCurrentChange(val) {
      this.searchForm.pageNum = val;
      this.searchList();
    },
    //查询表单接口
    searchList() {
      let params = {
        enterpriseName: this.searchForm.enterpriseName,
        beginTime: this.searchForm.beginTime,
        endTime: this.searchForm.endTime,
        pageNum: this.searchForm.pageNum,
        pageSize: this.searchForm.pageSize,
        phoneNumber: this.searchForm.phoneNumber,
      };
      if(params.phoneNumber && !authPhone(params.phoneNumber)) {
        return this.$message.error('请输入正确的手机号')
      }
      if (params.beginTime || params.endTime) {
        if (!timeFun(params.beginTime, params.endTime)) {
          return this.$message.error("结束时间必须大于开始时间哦");
        }
      }
      getHistoryList(params).then((res) => {
        this.tableData = res.data.list;
        this.total = res.data.total;
      });
    },
    //表单验证
    getlist(formName) {
      this.$refs[formName].validate((valid) => {

        if (valid) {
          this.searchForm.pageNum = 1
          this.searchList();
        } else {
          return false;
        }
      });
    },
  },
};
</script>

  <style lang="scss" scoped>

.mt10 {
  margin-top: 10px;
}
</style>
